FIND_PACKAGE(MyBoost)
FIND_PACKAGE(OpenSSL)
FIND_PACKAGE(GetSvnRevision)

INCLUDE_DIRECTORIES("${BOOST_INCLUDE_DIR}")
LINK_DIRECTORIES("${BOOST_LIBRARY_DIR}")
IF(MSVC)
	# Disable autolinking from asio->regex because we don't use it
	ADD_DEFINITIONS("-DBOOST_REGEX_NO_LIB")
ENDIF()

IF(NOT ANDROID)
	SET(ENABLE_LEGACY_SETTINGS ON  CACHE BOOL "Enable support for loading old settings files (requires boost serialization)")
ELSE()
	SET(ENABLE_LEGACY_SETTINGS OFF CACHE BOOL "Enable support for loading old settings files (requires boost serialization)")
ENDIF()

IF(ENABLE_LEGACY_SETTINGS)
	ADD_DEFINITIONS("-DENABLE_LEGACY_SETTINGS")
ENDIF()

ADD_SUBDIRECTORY(net-asio)
ADD_SUBDIRECTORY(util)
ADD_SUBDIRECTORY(network)

ADD_SUBDIRECTORY(gtk-gui)
ADD_SUBDIRECTORY(qt-gui)
ADD_SUBDIRECTORY(console-ui)

SET(BUILD_BENCHES OFF CACHE BOOL "Build benchmarks and tests. (bench directory)")
IF(BUILD_BENCHES)
	SUBDIRS(bench)
ENDIF()

IF(LINUX AND NOT ANDROID)
	SUBDIRS(linux)
ENDIF()

SET(BVERMAJOR "0" CACHE STRING "Major version")
SET(BVERMINOR "<SVNREV>" CACHE STRING "Minor version")
SET(BVERPATCH "<TIMESTAMP>" CACHE STRING "Patch version")
SET(BVERBTYPE "0" CACHE STRING "Build type")

SET(BVEREXTRA "" CACHE STRING "Extra version id")

SET(BUILDVER_FILE "${CMAKE_CURRENT_BINARY_DIR}/BuildVersion.h")
file(WRITE  "${BUILDVER_FILE}" "// generated version file\n")
file(APPEND "${BUILDVER_FILE}" "#define VERSION_MAJOR_STRING \"${BVERMAJOR}\"\n")
file(APPEND "${BUILDVER_FILE}" "#define VERSION_MINOR_STRING \"${BVERMINOR}\"\n")
file(APPEND "${BUILDVER_FILE}" "#define VERSION_PATCH_STRING \"${BVERPATCH}\"\n")
file(APPEND "${BUILDVER_FILE}" "#define VERSION_BTYPE_STRING \"${BVERBTYPE}\"\n")
file(APPEND "${BUILDVER_FILE}" "#define VERSION_EXTRA_STRING \"${BVEREXTRA}\"\n")

SET(UFTT_SOURCES
	Main.cpp

	Types.h
	Globals.h
	UFTTGui.h
	UFTTStatus.h          #UFTTStatus.cpp
	IFileReader.h
	IMemoryBuffer.h
	FReadFileReader.h     FReadFileReader.cpp
	FReadMemoryBuffer.h   FReadMemoryBuffer.cpp
	BuildString.h         BuildString.cpp
	UFTTCore.h            UFTTCore.cpp
	UFTTSettings.h        UFTTSettings.cpp
	UFTTSettingsLegacy.h  UFTTSettingsLegacy.cpp
	UFTTCommandLine.h     UFTTCommandLine.cpp
	AutoUpdate.h          AutoUpdate.cpp
	Platform.h            Platform.cpp
)

ADD_LIBRARY(uftt-core
	${UFTT_SOURCES}
)

SET(UFTT_LIBRARIES
	uftt-core
	uftt-network
	lib-net-asio
	lib-util
)

SET(UFTT_EXTERNAL_LIBRARIES
	${BOOST_SYSTEM_LIBRARY}
	${BOOST_THREAD_LIBRARY}
	${BOOST_SIGNALS_LIBRARY}
	${BOOST_DATETIME_LIBRARY}
	${BOOST_FILESYSTEM_LIBRARY}
)

IF(LINUX AND NOT ANDROID)
	Set(UFTT_LIBRARIES ${UFTT_LIBRARIES} lib-uftt-linux)
ENDIF()

IF(ENABLE_LEGACY_SETTINGS)
	Set(UFTT_EXTERNAL_LIBRARIES ${UFTT_EXTERNAL_LIBRARIES} ${BOOST_SERIALIZATION_LIBRARY})
ENDIF()

GetSvnRevision("${CMAKE_CURRENT_BINARY_DIR}/SvnRevision.h")

ADD_CUSTOM_COMMAND(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/BuildStringMacros.cpp"
	COMMAND "${CMAKE_COMMAND}"
	ARGS -E copy "${CMAKE_CURRENT_SOURCE_DIR}/BuildStringMacros.cpp" "${CMAKE_CURRENT_BINARY_DIR}/BuildStringMacros.cpp"
	COMMENT "VersionTarget"
	DEPENDS ${UFTT_LIBRARIES} SvnRevision.h
)

if(OPENSSL_FOUND AND NOT APPLE AND NOT ANDROID)
	add_definitions("-DUSE_OPENSSL")
	INCLUDE_DIRECTORIES("${OPENSSL_INCLUDE_DIR}")
	SET(UFTT_EXTERNAL_LIBRARIES ${UFTT_EXTERNAL_LIBRARIES} ${OPENSSL_LIBRARY})
endif()

SET(ICON_FILE "")
IF(WIN32)
	SET(ICON_FILE "qt-gui/Icons/icon.rc")
ENDIF()
IF(APPLE)
	SET(ICON_FILE "qt-gui/Icons/uftt.icns")
	SET_SOURCE_FILES_PROPERTIES(
		${ICON_FILE}
		PROPERTIES
		MACOSX_PACKAGE_LOCATION Resources
	)
ENDIF()

SET(EXECUTABLE_TARGETS "")

IF(ENABLE_QT_GUI)
	SET(UFTT_QT_LIBRARIES uftt-qt)
	SET(EXECUTABLE_TARGETS ${EXECUTABLE_TARGETS} uftt)

	SET(MACOSX_BUNDLE_INFO_STRING "Ultimate File Transfer Tool")
	SET(MACOSX_BUNDLE_ICON_FILE "uftt.icns")
	SET(MACOSX_BUNDLE_GUI_IDENTIFIER "UFTT")
	SET(MACOSX_BUNDLE_SHORT_VERSION_STRING "0.0.1a")
	SET(MACOSX_BUNDLE_LONG_VERSION_STRING "0.0.1 - alpha")

	ADD_EXECUTABLE(uftt WIN32 MACOSX_BUNDLE ${ICON_FILE} "${CMAKE_CURRENT_BINARY_DIR}/BuildStringMacros.cpp")

	IF(MSVC)
		SET_TARGET_PROPERTIES(uftt PROPERTIES
			LINK_FLAGS_RELEASE "/SUBSYSTEM:WINDOWS"
		)
	ENDIF()

	SET_TARGET_PROPERTIES(uftt PROPERTIES COMPILE_DEFINITIONS "ENABLE_QT_GUI;USE_QEXT_PLATFORM_H")

	TARGET_LINK_LIBRARIES(uftt
		${UFTT_LIBRARIES}
		${UFTT_EXTERNAL_LIBRARIES}
		${UFTT_QT_LIBRARIES}
	)

	INSTALL(TARGETS uftt
		RUNTIME DESTINATION bin
	)
ENDIF()

IF(ENABLE_GTK_GUI)
	SET(UFTT_GTK_LIBRARIES uftt-gtk)
	SET(EXECUTABLE_TARGETS ${EXECUTABLE_TARGETS} guftt)

	ADD_EXECUTABLE(guftt WIN32 ${ICON_FILE} "${CMAKE_CURRENT_BINARY_DIR}/BuildStringMacros.cpp")

	IF(MSVC)
		SET_TARGET_PROPERTIES(guftt PROPERTIES
			LINK_FLAGS_RELEASE "/SUBSYSTEM:WINDOWS"
		)
	ENDIF()

	SET_TARGET_PROPERTIES(guftt PROPERTIES COMPILE_DEFINITIONS ENABLE_GTK_GUI)

	TARGET_LINK_LIBRARIES(guftt
		${UFTT_LIBRARIES}
		${UFTT_EXTERNAL_LIBRARIES}
		${UFTT_GTK_LIBRARIES}
	)
	INSTALL(TARGETS guftt
		RUNTIME DESTINATION bin
	)
ENDIF()

IF(ENABLE_CONSOLE_UI)
	SET(UFTT_CONSOLE_LIBRARIES uftt-console)
	SET(EXECUTABLE_TARGETS ${EXECUTABLE_TARGETS} cuftt)

	ADD_EXECUTABLE(cuftt ${ICON_FILE} "${CMAKE_CURRENT_BINARY_DIR}/BuildStringMacros.cpp")

	IF(MSVC)
		SET_TARGET_PROPERTIES(cuftt PROPERTIES
			LINK_FLAGS_RELEASE "/SUBSYSTEM:CONSOLE"
		)
	ENDIF()

	TARGET_LINK_LIBRARIES(cuftt
		${UFTT_LIBRARIES}
		${UFTT_EXTERNAL_LIBRARIES}
		${UFTT_CONSOLE_LIBRARIES}
	)
	INSTALL(TARGETS cuftt
		RUNTIME DESTINATION bin
	)
ENDIF()

IF(EXISTS "${CMAKE_ROOT}/Modules/CPack.cmake" AND NOT APPLE)
	SET(CPACK_GENERATOR "DEB" "RPM")

	SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Ultimate File Transfer Tool")
	SET(CPACK_PACKAGE_VENDOR "uftt")
	SET(CPACK_PACKAGE_NAME "uftt")
	SET(CPACK_PACKAGE_CONTACT "simon.sasburg@gmail.com")
	SET(CPACK_PACKAGE_VERSION_MAJOR "0")
	SET(CPACK_PACKAGE_VERSION_MINOR "1")
	SET(CPACK_PACKAGE_VERSION_PATCH "${BVERPATCH}")
	#SET(CPACK_PACKAGE_INSTALL_DIRECTORY "uftt ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}")
	SET(CPACK_SOURCE_PACKAGE_FILE_NAME "uftt-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
	SET(CPACK_PACKAGE_EXECUTABLES "uftt" "UFTT Executable")

	SET(CPACK_RPM_PACKAGE_ARCHITECTURE ${CURRENT_ARCHITECTURE})
	SET(CPACK_RPM_PACKAGE_LICENSE GLPV2)

	SET(CPACK_DEBIAN_PACKAGE_DEPENDS
		#"libc6, libfontconfig1, libfreetype6, libgcc1, libice6, libpng12-0, libqt4-core, libqt4-gui, libsm6, libssl0.9.8, libstdc++6, libx11-6, libxcursor1, libxext6, libxi6, libxinerama1, libxrandr2, libxrender1, zlib1g"
		"libc6, libgcc1, libstdc++6, libqt4-gui >= (4.4.0), libssl0.9.8"
		#old:	"libc6 (>= 2.3.1-6), libgcc1 (>= 1:3.4.2-12)"
	)

	SET(CPACK_RPM_PACKAGE_REQUIRES
		"glibc, libgcc, libstdc++6, libqt4 >= 4.4.0, libopenssl0_9_8"
	)

	IF(UNIX)
		SET(CPACK_STRIP_FILES "bin/uftt")
		SET(CPACK_STRIP_FILES "bin/guftt")
		SET(CMAKE_INSTALL_PREFIX "/usr/local")
	ENDIF()

	# List executables
	INCLUDE(CPack)
ENDIF()
